package com.epi.one.main;

/**
 * @author dxy
 * @version 1.0
 */
import javax.swing.*;
import java.awt.*;

public class TodoListApp {
    private JFrame frame;
    private JPanel todoPanel;
    private JPanel donePanel;
    private JPanel inputPanel;

    public static void main(String[] args) {
        test();
    }

    public static void test() {
        SwingUtilities.invokeLater(TodoListApp::new);
    }

    public TodoListApp() {
        frame = new JFrame("待办清单");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 600);
        frame.setLayout(new BorderLayout());

        // 输入栏 + 按钮
        inputPanel = new JPanel(new BorderLayout());
        JTextField inputField = new JTextField();
        JButton addButton = new JButton("+");

        inputPanel.add(inputField, BorderLayout.CENTER);
        inputPanel.add(addButton, BorderLayout.EAST);

        frame.add(inputPanel, BorderLayout.NORTH);

        // 主区域
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 1));

        todoPanel = new JPanel();
        todoPanel.setLayout(new BoxLayout(todoPanel, BoxLayout.Y_AXIS));
        donePanel = new JPanel();
        donePanel.setLayout(new BoxLayout(donePanel, BoxLayout.Y_AXIS));

        mainPanel.add(new JScrollPane(todoPanel));
        mainPanel.add(new JScrollPane(donePanel));

        frame.add(mainPanel, BorderLayout.CENTER);

        JLabel todoLabel = new JLabel("📋 待做事项");
        JLabel doneLabel = new JLabel("✅ 已完成");
        todoPanel.add(todoLabel);
        donePanel.add(doneLabel);

        addButton.addActionListener(e -> {
            String text = inputField.getText().trim();
            if (!text.isEmpty()) {
                addTask(text, false);
                inputField.setText("");
            }
        });

        frame.setVisible(true);
    }

    private void addTask(String task, boolean done) {
        JPanel taskPanel = new JPanel(new BorderLayout());
        JCheckBox checkBox = new JCheckBox();
        checkBox.setSelected(done);
        JLabel taskLabel = new JLabel(task);
        JButton deleteButton = new JButton("❌");

        taskPanel.add(checkBox, BorderLayout.WEST);
        taskPanel.add(taskLabel, BorderLayout.CENTER);
        taskPanel.add(deleteButton, BorderLayout.EAST);

        if (done) {
            donePanel.add(taskPanel);
        } else {
            todoPanel.add(taskPanel);
        }

        // 状态切换
        checkBox.addActionListener(e -> {
            if (checkBox.isSelected()) {
                todoPanel.remove(taskPanel);
                donePanel.add(taskPanel);
            } else {
                donePanel.remove(taskPanel);
                todoPanel.add(taskPanel);
            }
            frame.revalidate();
            frame.repaint();
        });

        // 删除任务
        deleteButton.addActionListener(e -> {
            Container parent = taskPanel.getParent();
            parent.remove(taskPanel);
            frame.revalidate();
            frame.repaint();
        });

        frame.revalidate();
        frame.repaint();
    }
}

